<%--
  Created by IntelliJ IDEA.
  User: Albert Lee
  Date: 2020/5/26
  Time: 17:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>复选删除</title>
</head>


<body>

<%--判断是否登录--%>
<%
    String userid=(String)session.getAttribute("userid");
//    System.out.print("Session userID="+userid);

    if (userid!=null) {
%> 

 <h3>UserID：<%=(String)session.getAttribute("userid")%>, 在线中..</h3>
<hr>


    <%
        request.setCharacterEncoding("UTF-8");
        String[] Id = request.getParameterValues("delete_id");
        System.out.println(Id);

        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;

        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/talknewsdatabase";
        String user = "root";
        String password = "123456";
        conn = DriverManager.getConnection(url,user,password);

        int result = 0;
        stat = conn.createStatement();
    //    stat.executeUpdate("delete from student where id = " + id + "");
        rs = stat.executeQuery("select * from info");


       if(conn != null) {
            stat = conn.createStatement();
            for(String id : Id ){
    //            System.out.println( rs.getInt("id") );
                String sql = "delete from info where user_id='" +userid +  "' and id = " + id;
//                System.out.println(sql);
                result = stat.executeUpdate(sql);
                if(result > 0){
                    System.out.println("Finished.");
                }
            }

            stat.close();
            conn.close();
           out.print("<script>alert('批量删帖成功！');</script>");
        }else{
            out.print("<script>alert('批量删贴失败！');</script>");
        }
    %>

    <script>window.location='http://localhost:8080/showInfo.jsp';</script>

    <%
        if(rs != null) {
            rs.close();
            rs = null;
        }
        if(stat != null) {
            stat.close();
            stat = null;
        }
        if(conn != null) {
            conn.close();
            conn = null;
        }
    %>

<%
    } else {
        out.print("<script>alert('未成功登录！');window.location='http://localhost:8080/index.html';</script>");
    }
%>


</body>
</html>
